package com.ikangtai.shecare.stickycalendar.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.stickycalendar.bean.MoreDayState;
import com.ikangtai.shecare.stickycalendar.fragment.CalendarRecordFragment;
import com.ikangtai.shecare.stickycalendar.utils.DateUtils;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/5/19 0019.
 */
public class MonthCalendarAdpter extends CalendarBaseAdpter {
    private List<View> views;
    private Context context;

    private String month;
    private Handler os = null;

    private String strToDay = "";
    private Map<String, MoreDayState> mMap = new HashMap<>();

    public MonthCalendarAdpter(List<View> views, Context context, Map<String, MoreDayState> map) {
        this.views = views;
        this.context = context;
        this.mMap = map;
        //选中今天
        Calendar today = new GregorianCalendar();
        today.setTimeInMillis(System.currentTimeMillis());

        strToDay = DateUtils.getTagTimeStr(today);


        selectTime = DateUtils.getTagTimeStr(today);

    }

    public void setHandler(Handler os) {
        this.os = os;
    }

    @Override
    public int getCount() {
        return 2400;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }


    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ViewGroup view = (ViewGroup) views.get(position % views.size());
        int index = container.indexOfChild(view);
        if (index != -1) {
            container.removeView(view);
        }
        try {
            container.addView(view);
        } catch (Exception e) {

        }
        refresh(view, position, mMap);
        return view;
    }

    public void getTimeList(Map<String, MoreDayState> map) {
        this.mMap = map;
    }

    /**
     * 提供对外的刷新接口
     */
    public void refresh(ViewGroup view, int position, Map<String, MoreDayState> mMap) {
        //给view 填充内容

        //设置开始时间为本周日
        Calendar today = new GregorianCalendar();
        today.setTimeInMillis(System.currentTimeMillis());
        //距离当前时间的月数
        int month = 1200 - position;
        today.add(Calendar.MONTH, -month);
        view.setTag(today.get(Calendar.MONTH) + "");
        //找到这个月的第一天所在星期的周日
        today.add(Calendar.DAY_OF_MONTH, -(today.get(Calendar.DAY_OF_MONTH) - 1));


        int day_of_week = today.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        today.add(Calendar.DATE, -day_of_week);

        render(view, today);
    }

    /**
     * 渲染page中的view：7天
     */

    private void render(final ViewGroup view1, final Calendar today) {

        //一页显示一个月+7天，为42；
        for (int b = 0; b < 6; b++) {
            final ViewGroup view = (ViewGroup) view1.getChildAt(b);
            for (int a = 0; a < 7; a++) {
                final int dayOfMonth = today.get(Calendar.DAY_OF_MONTH);

                // int day_of_year=today.get(Calendar.DAY_OF_YEAR);
                final ViewGroup dayOfWeek = (ViewGroup) view.getChildAt(a);
                //((TextView) dayOfWeek.getChildAt(0)).setText(getStr(today.get(Calendar.DAY_OF_WEEK)));
                ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setText(dayOfMonth + "");

                ((ImageView) dayOfWeek.findViewById(R.id.imv_point)).setVisibility(View.INVISIBLE);
                //不是当前月的显示为灰色
                if ((Integer.parseInt((String) view1.getTag())) != today.get(Calendar.MONTH)) {
                    if ((Integer.parseInt((String) view1.getTag())) > today.get(Calendar.MONTH)) {
                        //下个月
                        dayOfWeek.setOnClickListener(lastLister);
                    } else {
                        //上个月
                        dayOfWeek.setOnClickListener(nextLister);
                    }
                    dayOfWeek.findViewById(R.id.cal_bg_normal).setBackgroundDrawable((context.getResources().getDrawable((R.drawable.re_circle_bg_white))));
                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.gray));
                    today.add(Calendar.DATE, 1);
                    dayOfWeek.setAlpha(0f);
                    continue;
                } else {
                    dayOfWeek.setAlpha(1.0f);
                }
                dayOfWeek.setTag(DateUtils.getTagTimeStr(today));

                dayOfWeek.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        selectTime = dayOfWeek.getTag().toString();
                        is = true;
                        //TODO:发消息，告诉Activity我改变选中的日期了
                        if (MonthCalendarAdpter.this.os != null) {
                            Message message = new Message();
                            Bundle bundle = new Bundle();
                            bundle.putString("selectTime", selectTime);
                            message.setData(bundle);
                            message.what = CalendarRecordFragment.selectTime;
                            os.sendMessage(message);
                            os.sendEmptyMessage(CalendarRecordFragment.change);
                        }


                        today.add(Calendar.DATE, -42);//因为已经渲染过42天，所以today往前推42天， 代表当前page重绘；

                        //恢复上个选中的day的状态

                        if (day != null) {
                            if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                                day.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                                day.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getNormalBg()));
                                ((TextView) day.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                            } else {
                                day.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_blue));
                                day.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_blue));
                                ((TextView) day.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));

                            }
                            //特殊情况:上个选中的day今天
                            if (strToDay.equals(tag)) {
                                if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                                    day.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(mMap.get(strToDay).getSelectorBg()));
                                    day.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_translate));
                                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                                } else {
                                    day.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_blue));
                                    day.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable((R.drawable.re_circle_bg_blue)));
                                    ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                                }

                            }
                        }

                        //显示的调用invalidate
                        dayOfWeek.invalidate();
                        render(view1, today);
                        is = false;

                    }

                });

                if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                    dayOfWeek.findViewById(R.id.imv_point).setVisibility(mMap.get(DateUtils.getTagTimeStr(today)).isFuck()
                            ? View.VISIBLE : View.INVISIBLE);

                }
                //事实今天
                if (strToDay.equals(DateUtils.getTagTimeStr(today))) {
                    if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getSelectorBg()));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_translate));

                    } else {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));

                    }

                    if (!selectTime.equals(strToDay)) {
                        today.add(Calendar.DATE, 1);
                        continue;
                    }

                } else {


                    if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackgroundDrawable((context.getResources().getDrawable(mMap.get(strToDay).getNormalBg())));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(mMap.get(DateUtils.getTagTimeStr(today)).getNormalTextColor()));
                    } else {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackgroundDrawable((context.getResources().getDrawable((R.drawable.re_circle_bg_white))));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));

                    }
                }


                //选中当天

                if (selectTime.equals(DateUtils.getTagTimeStr(today))) {
                    if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getSelectorBg()));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable((R.drawable.re_circle_bg_translate)));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                    } else {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_blue));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_blue));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.white));
                    }

                    day = dayOfWeek;
                    if (MonthCalendarAdpter.this.os != null) {
                        Message message = os.obtainMessage();
                        message.obj = b;
                        message.what = CalendarRecordFragment.change2;
                        os.sendMessage(message);
                    }
                    tag = selectTime;
                } else {
                    if (mMap.get(DateUtils.getTagTimeStr(today)) != null) {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable(mMap.get(DateUtils.getTagTimeStr(today)).getNormalBg()));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(mMap.get(DateUtils.getTagTimeStr(today)).getNormalTextColor()));
                    } else {
                        dayOfWeek.findViewById(R.id.cal_bg_big).setBackground(context.getResources().getDrawable(R.drawable.re_circle_bg_white));
                        dayOfWeek.findViewById(R.id.cal_bg_normal).setBackground(context.getResources().getDrawable((R.drawable.re_circle_bg_white)));
                        ((TextView) dayOfWeek.findViewById(R.id.ca_day)).setTextColor(context.getResources().getColor(R.color.black));
                    }
                }
                today.add(Calendar.DATE, 1);
            }

        }

    }


    Calendar today1 = new GregorianCalendar();

    View.OnClickListener nextLister = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (MonthCalendarAdpter.this.os != null) {

                Message message = os.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putInt("pagerNext", CalendarRecordFragment.pagerNext);
                bundle.putInt("nextSelectTime", today1.get(Calendar.DAY_OF_MONTH));
                message.setData(bundle);
                message.what = CalendarRecordFragment.pagerNext;
                os.sendMessage(message);
            }
        }
    };
    View.OnClickListener lastLister = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (MonthCalendarAdpter.this.os != null) {
                Message message = os.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putInt("pagerLast", CalendarRecordFragment.pagerLast);
                bundle.putInt("preSelectTime", today1.get(Calendar.DAY_OF_MONTH));
                message.what = CalendarRecordFragment.pagerLast;
                message.setData(bundle);
                os.sendMessage(message);
            }
        }
    };

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    private ViewGroup day = null;

    public ViewGroup getDay() {
        return day;
    }

    private String tag = "";

    public String getSelectTime() {
        return selectTime;
    }

    public void setSelectTime(String selectTime) {
        this.selectTime = selectTime;
    }


}
